package com.zxq.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxq.web.channel.SynchronismMessage;
import com.zxq.web.channel.platform.PlatformMessageServiceImpl;
import com.zxq.web.dto.PlatformCmdDTO;
import com.zxq.web.enums.ECodeEnum;
import com.zxq.web.enums.EPlatformCmdType;
import com.zxq.web.iot.domain.IotCmd;
import com.zxq.web.iot.domain.IotDevice;
import com.zxq.web.iot.service.impl.IotCmdServiceImpl;
import com.zxq.web.iot.service.impl.IotDeviceServiceImpl;
import com.zxq.web.iot.service.impl.IotProjectFrameServiceImpl;
import com.zxq.web.iot.service.impl.IotProjectServiceImpl;
import com.zxq.web.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 发送设备指令
 */
@RestController("deviceCmd")
@Slf4j
public class IOTDeviceCmdController {


    @Autowired
    PlatformMessageServiceImpl platformMessageService;

    /**
     * 产品对应的帧格式服务
     */
    @Autowired
    IotProjectFrameServiceImpl iotProjectFrameDoService;

    /**
     * 设备服务
     */
    @Autowired
    IotDeviceServiceImpl iotDeviceService;

    /**
     * 产品服务
     */
    @Autowired
    IotProjectServiceImpl iotProjectService;

    /**
     * 同步消息中心
     */
    @Autowired
    SynchronismMessage synchronismMessage;

    @Autowired
    IotCmdServiceImpl iotCmdService;


    /**
     * 所有与网络中心交互的消息，都走这个接口，下发，指令，结果，全部从这里处理
     * @param platformCmdDTO 下发指令信息
     * @return
     */
    @PostMapping("platformCmd")
    public ResultVO<PlatformCmdDTO> platformCmd(@RequestBody PlatformCmdDTO platformCmdDTO) {
        ResultVO<PlatformCmdDTO> resultVO = new ResultVO<>();
        checkDevice(platformCmdDTO,resultVO);
        IotCmd iotCmdDo = iotCmdService.selectIotCmdById(platformCmdDTO.getCmdId());
        if(iotCmdDo == null) {
            log.info("not find this cmd!");
            resultVO.setCode(ECodeEnum.NotFindCmd.getCode());
            resultVO.setMessage(ECodeEnum.NotFindCmd.getMsg());
            resultVO.setSuccess(false);
            return resultVO;
        }
        EPlatformCmdType platformCmdType = EPlatformCmdType.getType(iotCmdDo.getCmdType().intValue());
        if(platformCmdType == EPlatformCmdType.CMD) {
            PlatformCmdDTO resultDTO = platformMessageService.platformCmd(platformCmdDTO);
            if (resultDTO != null) {
                resultVO.setData(resultDTO);
                resultVO.setCode(ECodeEnum.Success.getCode());
                resultVO.setMessage(ECodeEnum.Success.getMsg());
                resultVO.setSuccess(true);
            }
        }
        if(platformCmdType == EPlatformCmdType.NOTICE) {
            platformMessageService.platformNotify(platformCmdDTO);
            resultVO.setCode(ECodeEnum.Success.getCode());
            resultVO.setMessage(ECodeEnum.Success.getMsg());
            resultVO.setSuccess(true);
        }
        if(platformCmdType == EPlatformCmdType.RESULT) {
            synchronismMessage.notifyPlatformMessage(platformCmdDTO.getDevId()
                    ,platformCmdDTO.getCmdId(),platformCmdDTO);
            resultVO.setCode(ECodeEnum.Success.getCode());
            resultVO.setMessage(ECodeEnum.Success.getMsg());
            resultVO.setSuccess(true);
        }


        return resultVO;
    }


    /**
     * 检查设备是否符合规范
     * @param platformCmdDTO 平台指令数据
     * @param resultVO 返回的数据格式
     */
    private void checkDevice(PlatformCmdDTO platformCmdDTO,ResultVO resultVO) {
        String devId =  platformCmdDTO.getDevId();
        IotDevice iotDeviceCondition = new IotDevice();
        iotDeviceCondition.setDevId(devId);
        List<IotDevice> iotDeviceDos = iotDeviceService.selectIotDeviceList(iotDeviceCondition);
        if(iotDeviceDos.size() == 0) {
            resultVO.setMessage(ECodeEnum.IllegalDevice.getMsg());
            resultVO.setCode(ECodeEnum.IllegalDevice.getCode());
            resultVO.setSuccess(false);
            log.info(String.format("devId:{0} not found!",devId));
        }
        IotDevice iotDeviceDo = iotDeviceDos.get(0);
        Long projectId = iotDeviceDo.getProjectId();
        if(projectId == null || iotProjectService.selectIotProjectById(projectId) == null) {
            resultVO.setMessage(ECodeEnum.NotFindProduct.getMsg());
            resultVO.setCode(ECodeEnum.NotFindProduct.getCode());
            resultVO.setSuccess(false);
            log.info(String.format("devId:{0} not belong any product!",projectId));
        }

    }




}
